package com.tjsinfo.tjsoa.mail.bo;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.Dao.QbcDao;
import com.tjsinfo.tjsoa.mail.dao.ITjsFoldMailDao;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmail;
import com.tjsinfo.tjsoa.mail.vo.TjsDeleteEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsDraft;
import com.tjsinfo.tjsoa.mail.vo.TjsDraftAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldEmailAnnex;
import com.tjsinfo.tjsoa.mail.vo.TjsFoldMail;
import com.tjsinfo.tjsoa.mail.vo.TjsemailReceived;

import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class TjsFoldMailBo implements ITjsFoldMailBo{
	private final boolean STARTTRUE=true;
	private final boolean STARTFALSE=false;
	private ITjsFoldMailDao tjsFoldMailDao;
	private ITjsDeleteEmailBo tjsDeleteEmailBo;
	private IQbcDao qbcDao;
	public ITjsDeleteEmailBo getTjsDeleteEmailBo() {
		return tjsDeleteEmailBo;
	}
	public void setTjsDeleteEmailBo(ITjsDeleteEmailBo tjsDeleteEmailBo) {
		this.tjsDeleteEmailBo = tjsDeleteEmailBo;
	}
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	public ITjsFoldMailDao getTjsFoldMailDao() {
		return tjsFoldMailDao;
	}
	public void setTjsFoldMailDao(ITjsFoldMailDao tjsFoldMailDao) {
		this.tjsFoldMailDao = tjsFoldMailDao;
	}
	 
	
	/**
	 * 添加文件夹邮件
	 * @param title
	 * @param value
	 * @param date
	 * @param type
	 * @param start
	 * @param read
	 * @param tag
	 * @param list
	 * @return
	 */
	public boolean addTjsFlodMail(String title, String value, Date date,
			Integer type, Integer start, Integer read, Integer tag,
			String foldid, List<TjsFoldEmailAnnex> list) {
		// TODO Auto-generated method stub
		try {
			TjsFoldMail tjsFoldMail = new TjsFoldMail();//文件夹的邮件
			TjsEmailFolder tjsEmailFolder = new TjsEmailFolder();
			tjsEmailFolder.setFolderId(foldid);
			tjsFoldMail.setTjsEmailFolder(tjsEmailFolder);
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				TjsFoldEmailAnnex tjsFoldEmailAnnex = (TjsFoldEmailAnnex) iterator.next();
				tjsFoldMail.addTjsFoldMailAnnex(tjsFoldEmailAnnex);
			}
			tjsFoldMail.setTjsmailDatetime(date);
			tjsFoldMail.setTjsmailRead(read);
			tjsFoldMail.setTjsmailStart(start);
			tjsFoldMail.setTjsmailTag(tag);
			tjsFoldMail.setTjsmailTitle(title);
			tjsFoldMail.setTjsmailType(type);
			tjsFoldMail.setTjsmailValue(value);
			
			List<TjsFoldMail> list1 = tjsFoldMailDao.findAll();
			if (list.size()>0) {
				tjsFoldMail.setSortid(list1.iterator().next().getSortid()+1);
			}else {
				tjsFoldMail.setSortid(1);
			}
			
			tjsFoldMailDao.save(tjsFoldMail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	/**
	 * 删除文件夹邮件
	 * @param mailid
	 * @return
	 */
	public boolean deleteTjsFoldMail(String mailid) {
		// TODO Auto-generated method stub
		 
		try {
			tjsFoldMailDao.delete(selectOneTjsFoldMail(mailid));
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 根据文件夹ID查询邮件
	 * @param foldid
	 * @return
	 */
	public TjsFoldMail selectOneTjsFoldMail(String mailid) {
		// TODO Auto-generated method stub
		return tjsFoldMailDao.selectOneTjsFoldMail(mailid);//文件夹的邮件
	}
	/**
	 * 根据文件夹ID查询邮件
	 * @param foldid
	 * @return
	 */
	public List<TjsFoldMail> selectTjsFoldMailByFoldId(String foldid) {
		// TODO Auto-generated method stub
		return tjsFoldMailDao.selectTjsFoldMailByFoldId(foldid);
	}
	/**
	 * 修改文件夹邮件
	 * @param id
	 * @param title
	 * @param value
	 * @param date
	 * @param type
	 * @param start
	 * @param read
	 * @param tag
	 * @param list
	 * @return
	 */
	public boolean updateTjsFlodMail(String id, String title, String value,
			Date date, Integer type, Integer start, Integer read, Integer tag,
			List<TjsFoldEmailAnnex> list) {
		// TODO Auto-generated method stub
		try {
			TjsFoldMail tjsFoldMail = selectOneTjsFoldMail(id);//文件夹的邮件
			tjsFoldMail.setTjsmailDatetime(date);
			tjsFoldMail.setTjsmailRead(read);
			tjsFoldMail.setTjsmailStart(start);
			tjsFoldMail.setTjsmailTag(tag);
			tjsFoldMail.setTjsmailTitle(title);
			tjsFoldMail.setTjsmailType(type);
			tjsFoldMail.setTjsmailValue(value);
			tjsFoldMailDao.update(tjsFoldMail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	/**
	 * 修改读写模式
	 */
	public boolean updateRead(String mailid, Integer read) {
		// TODO Auto-generated method stub
		try {
			TjsFoldMail tjsFoldMail =  selectOneTjsFoldMail(mailid);//文件夹的邮件
			tjsFoldMail.setTjsmailRead(read);
			tjsFoldMailDao.update(tjsFoldMail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	/**
	 * 修改类型
	 */
	public boolean updateType(String mailid, Integer start) {
		// TODO Auto-generated method stub
		try {
			TjsFoldMail tjsFoldMail =  selectOneTjsFoldMail(mailid);//文件夹的邮件
			tjsFoldMail.setTjsmailType(start);
			tjsFoldMailDao.update(tjsFoldMail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 修改类型list
	 */
	public boolean updateTypeList(String[] mailid, Integer start) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < mailid.length; i++) {
				if (mailid[i] != null) {
					TjsFoldMail tjsFoldMail =  selectOneTjsFoldMail(mailid[i] );
					tjsFoldMail.setTjsmailType(start);
					tjsFoldMailDao.update(tjsFoldMail);
				}
				
			}
			
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 修改标签
	 */
	public boolean updateTag(String mailid, Integer tag) {
		// TODO Auto-generated method stub
		try {
			TjsFoldMail tjsFoldMail =  selectOneTjsFoldMail(mailid);//文件夹的邮件
			tjsFoldMail.setTjsmailTag(tag);
			tjsFoldMailDao.update(tjsFoldMail);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 批量修改文件夹类型邮件的标签
	 * @param mailid
	 * @param tag
	 * @return
	 */
	public boolean updateTagList(String[] mailid,Integer tag)
	{
			try {
				for (int i = 0; i < mailid.length; i++) {
					if(mailid[i] != null)
					{
						updateTag(mailid[i] , tag);
					}
					
				}
				return STARTTRUE;
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return STARTFALSE;
			}
	}
	public boolean deleteTjsEmailFoldList(String[] array,String username) {
		// TODO Auto-generated method stub
		try {
			TjsDeleteEmail deleteEmail = null;
			for (int i = 0; i < array.length; i++) {

				TjsFoldMail received = tjsFoldMailDao.selectByIdSub(array[i]).get(0);//文件夹的邮件
				deleteEmail = new TjsDeleteEmail();//删除的邮件
				deleteEmail.setTismailRead(received.getTjsmailStart());
				deleteEmail.setTismailTag(received.getTjsmailTag());
				deleteEmail.setTjsmailDatatime(new Date());
				deleteEmail.setTjsmailStart(0);
				deleteEmail.setTjsmailTitle(received.getTjsmailTitle());
				deleteEmail.setTjsmailType(received.getTjsmailTag());
				deleteEmail.setTjsmailValue(received.getTjsmailValue());
				DetachedCriteria criteria =  DetachedCriteria.forClass(TSystemUser.class);
				criteria.add(Expression.eq("userName", username));
				List list =	qbcDao.getCriteria(criteria);
				TSystemUser user = (TSystemUser) list.get(0);
				deleteEmail.setTjsmailUserid(user.getUserId());
				deleteEmail.setTjsmailUsername(user.getUserName());
				deleteEmail.setTjsmailsendren(received.getTjsmailUsername());
				Set set2 = received.getTjsFoldEmailAnnexes();//文件夹的邮件附件
				for (Iterator iterator = set2.iterator(); iterator.hasNext();) {
					TjsFoldEmailAnnex annex = (TjsFoldEmailAnnex) iterator.next();
					TjsDeleteEmailAnnex emailAnnex = new TjsDeleteEmailAnnex();
					emailAnnex.setAnnexAddress(annex.getAnnexAddress());
					emailAnnex.setAnnexName(annex.getAnnexName());
					emailAnnex.setAnnexSize(annex.getAnnexSize());
					emailAnnex.setZhenname(annex.getAnnexZhenName());
					deleteEmail.addAnnexes(emailAnnex);
				}
				tjsDeleteEmailBo.addTjsDeleteEmail(deleteEmail);
				deleteTjsFoldMail(array[i]);
			}
			return STARTTRUE;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}
	/**
	 * 修改文件夹型邮件的读写模式
	 */
	public boolean updateReadList(String[] mailid, Integer read) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < mailid.length; i++) {
				if(mailid[i] != null)
				{
					updateRead(mailid[i],read);
				}
				
			}
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	public boolean updateStart(String mailid, Integer start) {
		// TODO Auto-generated method stub
		return false;
	}
	/**
	 * 查询一个邮件
	 * @param id
	 * @return
	 */
	public TjsFoldMail selectOneByIdSUB(String id)
	{
		List<TjsFoldMail> list = tjsFoldMailDao.selectByIdSub(id);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	/**
	 * 根据标题查询
	 * @param object[标题，文件夹ID，用户id]
	 * @return
	 */
	public List<TjsFoldMail> selectByTitle(String title,String foldid,Integer userid)
	{
		Object[] object = {"%"+title+"%",foldid,userid};
		return  tjsFoldMailDao.selectByTitle(object);
	}
}
